from aiogram_dialog import Dialog, Window
from aiogram_dialog.widgets.input import TextInput
from aiogram_dialog.widgets.kbd import Back, Button, Calendar, Cancel, Group
from aiogram_dialog.widgets.text import Const, Format

from src.presentation.tg_bot.dialogs.widgets import LocaleText
from src.presentation.tg_bot.states import MailingSG

from ...getters.mailing import get_current_time
from .handlers import (
    confirm_mailing,
    set_mailing_date,
    set_mailing_settings,
    set_mailing_text,
    set_mailing_text_eng,
    set_mailing_time,
)

mailing_start_window = Window(
    Const("По кому делать рассылку?"),
    Group(
        Button(Const("По всем участникам"), id="all_users", on_click=set_mailing_settings),
        Button(
            Const("По незарегистрированным"),
            id="not_registered",
            on_click=set_mailing_settings,
        ),
        Button(
            Const("По зарегистрированным"),
            id="registered",
            on_click=set_mailing_settings,
        ),
        width=2,
    ),
    Cancel(LocaleText("back-btn")),
    state=MailingSG.start_mailing,
)

mailing_text_window = Window(
    Const("Введи текст рассылки"),
    TextInput("mailing_text", str, on_success=set_mailing_text),
    Back(LocaleText("back-btn")),
    state=MailingSG.mailing_text,
)

mailing_text_eng_window = Window(
    Const("Введи текст рассылки для англоязычных пользователей"),
    TextInput("mailing_text_eng", str, on_success=set_mailing_text_eng),
    Back(LocaleText("back-btn")),
    state=MailingSG.mailing_text_eng,
)

select_date_window = Window(
    Const("Выбери дату рассылки"),
    Calendar(id="calendar", on_click=set_mailing_date),
    Back(LocaleText("back-btn")),
    state=MailingSG.select_date,
)

select_time_window = Window(
    Const("Выбери время рассылки в формате 23:59:59"),
    Format("Текущее время: {current_time}"),
    TextInput("mailing_time", str, on_success=set_mailing_time),
    Back(LocaleText("back-btn")),
    state=MailingSG.select_time,
    getter=get_current_time,
)

confirm_mailing_window = Window(
    Const("Подтвердить рассылку?"),
    Button(Const("Да"), id="yes", on_click=confirm_mailing),
    Button(Const("Нет"), id="no", on_click=confirm_mailing),
    Back(LocaleText("back-btn")),
    state=MailingSG.confirm_mailing,
)

mailing_dialog = Dialog(
    mailing_start_window,
    mailing_text_window,
    mailing_text_eng_window,
    select_date_window,
    select_time_window,
    confirm_mailing_window,
)
